import {PlotTypes} from '../../enum/PlotTypes';
import * as PlotUtils from '../../utils';
import {Point} from '../../type';

// @ts-ignore
class Circle extends ol.geom.Polygon {
    public params: any;
    public isPlot: boolean = true;
    public fixPointCount: number = 2;

    private readonly _type: PlotTypes;
    private points: Array<Point>;

    constructor(coordinates: any, points: Array<Point>, params: any) {
        super([]);
        this._type = PlotTypes.CIRCLE;
        this.points = [];
        this.params = params;
        if (points && points.length > 0) {
            if (params.radius) {
                const newPoint: Point = [points[0][0] + params.radius, points[0][1]];
                points.push(newPoint);
                this.setPoints(points);
            } else {

            }
            this.setPoints(points);
        } else if (coordinates && coordinates.length > 0) {
            // @ts-ignore
            this.setCoordinates(coordinates);
        }
    }

    /**
     * 获取标绘类型
     * @return {PlotTypes}
     */
    public getPlotType(): PlotTypes {
        return this._type;
    }

    /**
     * 获取坐标点
     * @return {Array<Point>}
     */
    public getPoints(): Array<Point> {
        return this.points.slice(0);
    }

    /**
     * 获取点数
     * @return {number}
     */
    public getPointCount(): number {
        return this.points.length;
    }

    public getRadius(): number {
        return PlotUtils.mathDistance(this.points[0], this.points[1]);
    }

    public getCenter(): Point {
        return this.points[0];
    }

    /**
     * 更新当前坐标
     * @param point
     * @param index
     */
    public updatePoint(point: Point, index: number): void {
        if (index >= 0 && index < this.points.length) {
            this.points[index] = point;
            this.generate();
        }
    }

    /**
     * 更新最后一个坐标
     * @param point
     */
    public updateLastPoint(point: Point): void {
        this.updatePoint(point, this.points.length - 1);
    }

    /**
     * 完成绘制
     */
    public finishDrawing(): void {

    }

    private setPoints(value: Array<Point>): void {
        this.points = !value ? [] : value;
        if (this.points.length >= 1) {
            this.generate();
        }
    }

    private generate(): void {
        const count: number = this.getPointCount();
        if (count < 2) {
            return;
        } else {
            const center: Point = this.points[0];
            const radius: number = PlotUtils.mathDistance(center, this.points[1]);
            // @ts-ignore
            this.setCoordinates([this.generatePoints(center, radius)]);
        }
    }

    private generatePoints(center: Point, radius: number): Array<Point> {
        let [x, y, angle] = [0, 0, 0];
        const points: Array<Point> = [];
        for (let i: number = 0; i <= 100; i++) {
            angle = Math.PI * 2 * i / 100;
            x = center[0] + radius * Math.cos(angle);
            y = center[1] + radius * Math.sin(angle);
            points.push([x, y]);
        }
        return points;
    }
}

export default Circle;